package com.example.filecabinet.service.impl;

import com.example.filecabinet.constant.ErrorConstant;
import com.example.filecabinet.constant.RecordConstant;
import com.example.filecabinet.controller.LatticeController;
import com.example.filecabinet.dao.Device_infoMapper;
import com.example.filecabinet.dao.Lattice_infoMapper;
import com.example.filecabinet.entity.Device_info;
import com.example.filecabinet.entity.Device_infoExample;
import com.example.filecabinet.entity.Lattice_info;
import com.example.filecabinet.entity.Lattice_infoExample;
import com.example.filecabinet.service.DeviceService;
import com.example.filecabinet.service.Lattice_infoService;
import com.example.filecabinet.util.AutoIdFactory;
import com.thfdcsoft.framework.common.date.DateFormatConstant;
import com.thfdcsoft.framework.common.date.DateFormatFactory;
import com.thfdcsoft.framework.common.http.dto.BaseHttpRspn;
import com.thfdcsoft.framework.common.json.jackson.JacksonFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 设置设备
 *
 * @author wangy
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private Lattice_infoService lattice_infoService;
    @Autowired
    private Device_infoMapper device_infoMapper;
    @Autowired
    private static Lattice_infoMapper lattice_infoMapper;

    @Override
    public String addCabinet(Device_info device_info) {
        BaseHttpRspn result = new BaseHttpRspn(false);
        String deviceNumber = device_info.getDeviceNumber();
//        if (device_infoMapper.countByDeviceNumber(deviceNumber) != 0) {
//            result.setRespMsg(ErrorConstant.DEVICE.ADD_ERROR);
//        } else {
//            device_infoMapper.insertSelective(device_info);
//            int count = Integer.parseInt(device_info.getDeviceCount());
//            System.out.println("count" + count);
//            for (int i = 1; i <= count; i++) {
//                lattice_infoService.addLattice(i, deviceNumber);
//            }
//            result.setResult(true);
//        }
        Device_info device_info1 = device_infoMapper.selectByDeviceNumber(deviceNumber);
        int total = Integer.valueOf(device_info1.getDeviceCount());
        int newCount = 0;
        if(total!=0){
           newCount = Integer.valueOf(device_info.getDeviceCount());
           total = total + newCount;
            device_info1.setDeviceCount(total+"");
            int count = device_infoMapper.updateByPrimaryKeySelective(device_info1);
            if(count!=0){
                System.out.println("count" + count);
            for (int i = 1; i <= count; i++) {
                lattice_infoService.addLattice(i, deviceNumber);
            }
                result.setResult(true);
            }
        }
        return JacksonFactory.writeJson(result);
    }

    @Override
    public String updateCabinet(Device_info updateDevice) {
        String deviceNumber = updateDevice.getDeviceNumber();
        Device_info deviceInfo = device_infoMapper.selectByDeviceNumber(deviceNumber);
        updateDevice.setDeviceId(deviceInfo.getDeviceId());
        BaseHttpRspn result = new BaseHttpRspn(false);
        if (deviceInfo != null) {
            int oddCount = Integer.parseInt(deviceInfo.getDeviceCount());
            int newCount = Integer.parseInt(updateDevice.getDeviceCount());
            if (newCount > oddCount) {
                for (int i = oddCount + 1; i <= newCount; i++) {
                    lattice_infoService.addLattice(i, deviceNumber);
                }
                device_infoMapper.updateByPrimaryKeySelective(updateDevice);
                result.setResult(true);
            }else{
                // 查询是否要删除的格子是空的
                boolean isUsing=lattice_infoService.selectLatticeUsing(oddCount,newCount,deviceNumber);
                if (isUsing){
                    result.setResult(false);
                    result.setRespMsg("该文件柜中有文件未取出");
                }else{
                    lattice_infoService.deleteByExample(oddCount,newCount,deviceNumber);
                    device_infoMapper.updateByPrimaryKeySelective(updateDevice);
                    result.setResult(true);
                }
            }
        } else {
            result.setResult(false);
            result.setRespMsg("请先添加该文件柜");
        }
        return JacksonFactory.writeJson(result);
    }

    @Override
    public String selectCabint(Device_info device_info) {
        String deviceNumber = device_info.getDeviceNumber();
        Device_info deviceInfo = device_infoMapper.selectByDeviceNumber(deviceNumber);
        device_info.setDeviceId(deviceInfo.getDeviceId());
        BaseHttpRspn result = new BaseHttpRspn(false);
        if (deviceInfo != null) {
            result.setRespJson(JacksonFactory.writeJson(deviceInfo));
            result.setResult(true);
        } else {
            result.setRespMsg("未查询到该文件柜");
        }
        return JacksonFactory.writeJson(result);
    }


}
